﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Skymate.Reids.Sentinel
{
    using System.Configuration;
    using System.Diagnostics;
    using System.Threading;

    using Abp;
    using Abp.Dependency;
    using Abp.Extensions;
    using Abp.Runtime.Caching;

    using StackExchange.Redis;

    class Program
    {
        private static void SentinelRedisTest()
        {
            // 设置 redis 集群服务器地址，目前不知道如何自动切换，所以只能给出所有可连接的地址
            var ips = new[]
                          {
                            "localhost:6379",
                            "localhost:6380",
                            "localhost:6381"
                          };

            var connectionip = string.Join(",", ips);

            ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(connectionip);

            IDatabase db = redis.GetDatabase();

            // 设置 db 值
            string value = "abcdefg";
            db.StringSet("mykey", value);
            string message = "Item value is: " + db.StringGet("mykey");
            Console.WriteLine(message);

            Stopwatch watch = new Stopwatch();
            watch.Start();
            while (true)
            {
                Thread.Sleep(500); // 等待500ms
                // 每500 ms取一次
                message = DateTime.Now.ToString("HH:mm:ss.fff") + " Item value is: " + db.StringGet("mykey");
                Console.WriteLine(message);
                if (watch.ElapsedMilliseconds > 60 * 1000)
                {
                    // 运行 1min
                    break;
                }
            }

            watch.Stop();
            
        }

        private static ICacheManager _cacheManager;

        static void Main(string[] args)
        {
            InitConsole();
            InitAbp();
            
            var randomValue = GetCacheItem();
            Console.WriteLine("第一次取值:{0}",randomValue);
            
            Stopwatch watch = new Stopwatch();
            watch.Start();
            while (true)
            {
                Thread.Sleep(5000); // 等待500ms
                // 每500 ms取一次
                randomValue = GetCacheItem();
                Console.WriteLine("{1}取值:{0}", randomValue, DateTime.Now.ToString("HH:mm:ss.fff"));

                if (watch.ElapsedMilliseconds > 10*60 * 1000)
                {
                    // 运行 1min
                    break;
                }
            }

            watch.Stop();

            Console.ReadKey(true);
        }

        private static void InitConsole()
        {
            var title= ConfigurationManager.AppSettings["title"];
            Console.Title = title.IsNullOrEmpty() ? "master" : title;
            //Console.Title = "Test";
        }

        /// <summary>
        ///     初始化abp
        /// </summary>
        private static void InitAbp()
        {
            var bootstrapper = AbpBootstrapper.Create<ReidsSentinelCacheModule>();
            bootstrapper.Initialize();
            _cacheManager= IocManager.Instance.Resolve<ICacheManager>();
        }

        /// <summary>
        ///     获取缓存值
        /// </summary>
        /// <returns>随机值</returns>
        public static int GetCacheItem()
        {
            var cache = _cacheManager.GetCache("RANDOMCACHE");
            
            return int.Parse((cache.Get(
                "RandomValue",
                () =>
                {
                    Console.WriteLine("非缓存数据");
                    Random rd = new Random();
                    return rd.Next(1, 100000);
                })).ToString());
        }

        private static void ClearCache()
        {
            var cache = _cacheManager.GetCache("RANDOMCACHE");
            cache.Clear();
        }

    }
}
